package com.cloudeasy.area.listener;

import com.cloudeasy.api.constant.AmqpConstant;
import com.cloudeasy.api.util.JsonUtil;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.amqp.AreaItemIncVO;
import com.cloudeasy.area.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class ItemListener {
    private final AreaService areaService;

    public ItemListener(AreaService areaService) {
        this.areaService = areaService;
    }

    @RabbitListener(queuesToDeclare = @Queue(AmqpConstant.AreaConstant.AREA_ITEM_ROUTING_KEY))
    public void process(String message) {
        AreaItemIncVO areaItemIncVO = JsonUtil.fromJson(message, AreaItemIncVO.class);
        log.info("区域 {} 项变更 {}", Objects.requireNonNull(areaItemIncVO).getAreaName(), areaItemIncVO.getInc());
        areaService.itemIncByAreaName(areaItemIncVO.getAreaName(), areaItemIncVO.getInc());
    }
}
